package com.example.wendao.view.ui.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.databinding.ActivityMainBinding;
import com.example.wendao.databinding.FragmentMyBinding;
import com.example.wendao.model.db.dao.UserDao;
import com.example.wendao.model.entity.Fans;
import com.example.wendao.model.entity.User;
import com.example.wendao.viewmodel.UserViewModel;

public class MyFragment extends Fragment {
    private FragmentMyBinding mBinding;
    private View view;
    private UserViewModel userViewModel;
    public MyFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_my, container, false);
        view = mBinding.getRoot();

        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);

        User user = userViewModel.getCurrentUser();

        if (user != null) {
            mBinding.myPersonalgoodsNumber.setText("8");
            mBinding.myUsernameTextview.setText(user.getUsername());

            userViewModel.getLiveCurrentUser().observe(this.getViewLifecycleOwner(), user1 -> mBinding.myUsernameTextview.setText(user1.getUsername()));

            mBinding.myFansnumberTextview.setText("3");
            if (user.isGender()) {
                mBinding.myGenderImagview.setImageResource(R.drawable.male);
            }
            mBinding.myCoinnumberTextview.setText(String.valueOf(user.getCoins()));
        } else {
            mBinding.myUsernameTextview.setText("去登录");
        }
        mBinding.myUsernameTextview.setOnClickListener(view -> Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_loginFragment));

        //查看金币
        mBinding.myCionTextview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
            Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_sign_inFragment);
            else showMessage();
        });
        mBinding.myFansTextview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_fragment_fans);
            else showMessage();
        });
        //查看粉丝
        mBinding.myFansTextview.setOnClickListener(view -> Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_fragment_fans));
        //查看我的关注
        mBinding.myWatchTextview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_findfollowerFragment);
            else showMessage();
        });
        //修改个人信息
        mBinding.myPersonalimgImagview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_modifyUserInfoFragment);
            else showMessage();
        });
        //点击用户物品的跳转
        mBinding.myPersonalgoodsTextview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_goodsFragment);
            else showMessage();
        });
        //查看金币页面
        mBinding.myCionTextview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_sign_inFragment);
            else showMessage();
        });
        //查看关注页面
        mBinding.myWatchTextview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_findfollowerFragment);
            else showMessage();
        });
        //查看点赞页面
        mBinding.myAgreeImageView.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_agreeFragment);
            else showMessage();
        });
        //查看足迹页面
        mBinding.myHistroyImagview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_fragment_history);
            else showMessage();
        });
        //查看收藏界面
        mBinding.myCollectionImagview.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_fragment_collect);
            else showMessage();
        });
        mBinding.myShopIamgeView.setOnClickListener(view -> {
            if (userViewModel.userLoggedIn())
                Navigation.findNavController(view).navigate(R.id.action_fragment_my_to_fragment_mall);
            else showMessage();
        });

        return view;
    }

    private void showMessage(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setPositiveButton("确认", (dialog, which) -> {});
        builder.setTitle("提示");
        builder.setMessage("请登录之后查看此页面!");
        builder.show();
    }

}
